package com.example.study.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.study.design.factory.KeyMethodFactory;
import com.example.study.design.factory.KeyMethodFactoryMap;
import com.example.study.design.factory.KeySimpleFactory;
import com.example.study.service.KeyService;
import com.example.study.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 钥匙controller层
 * 假装有：@RestController，@RequestMapping("/key")
 * 请忽略：@Component
 *
 * @author duckquan
 */
@Component
public class KeyController {

    @Autowired
    private KeyMethodFactoryMap keyMethodFactoryMap;

    /**
     * 简单工厂模式-开通钥匙接口，按照类型开通不同的钥匙
     * 假装有：@PostMapping("/openKey")
     *
     * @param id   钥匙id
     * @param type 1-蓝牙钥匙，2-分享钥匙，3-手表钥匙
     * @return
     */
    public ResultUtil<Object> openKey(String id, Integer type) {
        //TODO 这里自行判断参数是否为空
        //--------------------------------
        KeyService service = KeySimpleFactory.getInstance(type);
        return ResultUtil.successResult(JSONObject.toJSONString(service.openKey(id)));
    }

    /**
     * 工厂方法模式-开通钥匙接口，按照类型开通不同的钥匙
     * 假装有：@PostMapping("/methodFactoryOpenKey")
     *
     * @param id   钥匙id
     * @param type 1-蓝牙钥匙，2-分享钥匙，3-手表钥匙
     * @return
     */
    public ResultUtil<Object> methodFactoryOpenKey(String id, Integer type) {
        //TODO 这里自行判断参数是否为空
        //--------------------------------
        //获取工厂
        KeyMethodFactory factory = keyMethodFactoryMap.getFactory(type);
        //获取产品
        KeyService service = factory.getInstance();
        return ResultUtil.successResult(JSONObject.toJSONString(service.openKey(id)));
    }

}
